package com.supcon.exchangedata.service.gj;

import com.supcon.exchangedata.pojo.entity.mysql.operation.gj.*;
import com.supcon.exchangedata.pojo.entity.oracle.*;
import com.supcon.exchangedata.repository.master.*;
import com.supcon.exchangedata.repository.slave4.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 * 公交基础数据
 *
 * @Author: yangbin
 * @Date: 2022/12/26/9:27
 * @Description:
 */
@Service
@Slf4j
public class GjBasicService {
    @Resource
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Resource
    private MasterGisPointRepository masterGisPointRepository;
    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;
    @Resource
    private SlaveGjBasicInfo slaveGjBasicInfo;
    @Resource
    private SlaveGjBasicInfo1 slaveGjBasicInfo1;
    @Resource
    private MasterBusRouteInfoRepository busRouteInfoRepository;
    @Resource
    private SlaveGjBasicInfo2 slaveGjBasicInfo2;
    @Resource
    private MasterBusRouteInfoStationRepository masterBusRouteInfoStationRepository;

    @Resource
    private SlaveGjBasicInfo4 slaveGjBasicInfo4;
    @Resource
    private SlaveGjBasicInfo3 slaveGjBasicInfo3;
    @Resource
    private SlaveGjBasicInfo5 slaveGjBasicInfo5;

    @Transactional(rollbackFor = Exception.class)
    public void updateBikeStation() {
        long beginAll = System.currentTimeMillis();

        List<SgjjtBike> all = slaveGjBasicInfo.findAll();
        if (!CollectionUtils.isEmpty(all)) {

            //删除 1002
            //查找自行车站点数据
            List<BaseGeoArea> baseGeoAreaList = masterBaseAreaRepository.findBaseGeoAreaByAreaType(1002L);
            List<GisPoint> allByBusinessCategory = masterGisPointRepository.findAllByBusinessCategory("1002");
            List<BaseGeoAreaDetail> detailList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(baseGeoAreaList)) {
                baseGeoAreaList.forEach((e) -> {
                    BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                    baseGeoAreaDetail.setAreaId(e.getId());
                });
                long begin = System.currentTimeMillis();
                masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(1002L);
                long end = System.currentTimeMillis();
                System.out.println("baseGeo删除时长：" + (end - begin) + "ms");

                long begin1 = System.currentTimeMillis();
                masterBaseGeoAreaDetailRepository.deleteInBatch(detailList);
                long end1 = System.currentTimeMillis();
                System.out.println("baseGeoDetail删除时长：" + (end1 - begin1) + "ms");
            }
            //删除对应表
            if (!CollectionUtils.isEmpty(allByBusinessCategory)) {
                long begin = System.currentTimeMillis();
                masterGisPointRepository.deleteGisPointByBusinessCategory("1002");
                long end = System.currentTimeMillis();
                System.out.println("gisPoint删除时长：" + (end - begin) + "ms");
            }

            List<BaseGeoArea> addBaseGeoList = new ArrayList<>();
            List<GisPoint> addGisPointList = new ArrayList<>();
            List<BaseGeoAreaDetail> addBaseGeoDetailList = new ArrayList<>();
            for (SgjjtBike sgjjtBike : all) {
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                //地区
                baseGeoAreaDetail.setServiceArea(sgjjtBike.getAreaname());
                //桩位总数
                baseGeoAreaDetail.setDeviceTotal(0L);
                baseGeoAreaDetail.setSubsidiaryInformation4("无桩");
                try {
                    Long total = Long.valueOf(sgjjtBike.getTotalcount());
                    baseGeoAreaDetail.setDeviceTotal(total);
                    if (total > 0) {
                        baseGeoAreaDetail.setSubsidiaryInformation4("有桩");
                    }

                } catch (Exception e) {
                }
                baseGeoAreaDetail.setSubsidiaryInformation1(sgjjtBike.getRentcount());

                baseGeoAreaDetail.setSubsidiaryInformation2(sgjjtBike.getRestorecount());
                // 7 正常 8 异常 9 掉线
                String status = sgjjtBike.getStatus();
                String statusTarget = "异常";
                switch (status) {
                    case "7":
                        statusTarget = "正常";
                        break;
                    case "9":
                        statusTarget = "掉线";
                        break;
                }
                baseGeoAreaDetail.setSubsidiaryInformation3(statusTarget);
                //编辑地区表

                BaseGeoArea baseGeoArea = new BaseGeoArea();
                baseGeoArea.setUpdator("jpa-program");
                baseGeoArea.setUpdateDate(LocalDateTime.now());
                baseGeoArea.setAreaType(1002L);
                baseGeoArea.setAreaCode("gjzxc" + sgjjtBike.getTongid());
                baseGeoArea.setParentId(20874L);
                baseGeoArea.setAddress(sgjjtBike.getAddress());
                baseGeoArea.setAreaName(sgjjtBike.getDevicename());
                baseGeoArea.setTrueName(sgjjtBike.getDevicename());
                String coordinate = sgjjtBike.getCoordinate();
                if (!StringUtils.isEmpty(coordinate)) {
                    String[] split = coordinate.split(",");
                    if (split.length == 2) {
                        baseGeoArea.setLatitude(Double.valueOf(split[1]));
                        baseGeoArea.setLongitude(Double.valueOf(split[0]));
                    }
                }
                baseGeoArea.setBaseGeoAreaDetail(baseGeoAreaDetail);
                addBaseGeoList.add(baseGeoArea);

            }
            long begin = System.currentTimeMillis();
            List<BaseGeoArea> targetList = masterBaseAreaRepository.saveAll(addBaseGeoList);
            long end = System.currentTimeMillis();
            System.out.println("BaseGeoArea批量插入时长：" + (end - begin) + "ms");


            for (BaseGeoArea baseGeoArea : targetList) {
                BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();
                baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                addBaseGeoDetailList.add(baseGeoAreaDetail);

                GisPoint gisPoint = new GisPoint();
                gisPoint.setObjId(baseGeoArea.getId());
                gisPoint.setYwCategory("1002");
                gisPoint.setObjType(1L);
                gisPoint.setLongitude(baseGeoArea.getLongitude());
                gisPoint.setLatitude(baseGeoArea.getLatitude());
                gisPoint.setName(baseGeoArea.getAreaName());
                gisPoint.setCode(baseGeoArea.getAreaCode());
                gisPoint.setBusinessCategory("1002");
                addGisPointList.add(gisPoint);


            }


            long begin1 = System.currentTimeMillis();
            masterBaseGeoAreaDetailRepository.saveAll(addBaseGeoDetailList);
            long end1 = System.currentTimeMillis();
            System.out.println("BaseGeoDetail批量插入时长：" + (end1 - begin1) + "ms");

            long begin2 = System.currentTimeMillis();
            masterGisPointRepository.saveAll(addGisPointList);
            long end2 = System.currentTimeMillis();
            System.out.println("gisPoint批量插入时长：" + (end2 - begin2) + "ms");
            long endAll = System.currentTimeMillis();
            System.out.println("总耗时：" + (endAll - beginAll) / 1000 + "秒");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void busRouteInfo() {
        List<SgjjtRoute> allBusRoute = slaveGjBasicInfo1.findAllBusRoute();
        if (!CollectionUtils.isEmpty(allBusRoute)) {
            //公交线路
            busRouteInfoRepository.deleteAll();
            List<CtBusRouteInfo> busRouteInfoList = new ArrayList<>();
            for (SgjjtRoute sgjjtRoute : allBusRoute) {
                CtBusRouteInfo ctBusRouteInfo = new CtBusRouteInfo();
                ctBusRouteInfo.setRouteid(sgjjtRoute.getRouteid());
                ctBusRouteInfo.setSubrouteid(sgjjtRoute.getSubrouteid());
                ctBusRouteInfo.setSegmentid(sgjjtRoute.getSegmentid());
                ctBusRouteInfo.setRoutename(sgjjtRoute.getRoutename());
                ctBusRouteInfo.setRoutetype(sgjjtRoute.getRoutetype());
                ctBusRouteInfo.setFstsendtime(sgjjtRoute.getFstsendtime());
                ctBusRouteInfo.setLstsendtime(sgjjtRoute.getLstsendtime());
                ctBusRouteInfo.setSegmentname(sgjjtRoute.getSegmentname());
                ctBusRouteInfo.setSngmile(Long.valueOf(sgjjtRoute.getSngmile()));
                ctBusRouteInfo.setSubid(sgjjtRoute.getSubid());
                ctBusRouteInfo.setBizTime(sgjjtRoute.getBizTime());
                ctBusRouteInfo.setLoadTime(sgjjtRoute.getTongTime());
                busRouteInfoList.add(ctBusRouteInfo);
            }
            busRouteInfoRepository.saveAll(busRouteInfoList);
        }
    }

    @Transactional
    public void busRouteInfoStation() {
        List<SgjjtRouteStop> allBusRouteStation = slaveGjBasicInfo2.findAllBusRouteStation();
        if (!CollectionUtils.isEmpty(allBusRouteStation)) {
            masterBusRouteInfoStationRepository.deleteAll();
            List<CtBusRouteStation> busRouteStationList = new ArrayList<>();
            for (SgjjtRouteStop sgjjtRouteStop : allBusRouteStation) {
                CtBusRouteStation ctBusRouteStation = new CtBusRouteStation();
                ctBusRouteStation.setStationid(sgjjtRouteStop.getStationid());
                ctBusRouteStation.setRouteid(sgjjtRouteStop.getRouteid().longValue());
                ctBusRouteStation.setDualserialid(sgjjtRouteStop.getDualserialid().intValue());
                ctBusRouteStation.setStationtype(sgjjtRouteStop.getStationtype());
                ctBusRouteStation.setLongitude(sgjjtRouteStop.getLongitude());
                ctBusRouteStation.setLatitude(sgjjtRouteStop.getLatitude());
                ctBusRouteStation.setStationname(sgjjtRouteStop.getStationname());
                ctBusRouteStation.setRoutename(sgjjtRouteStop.getRoutename());
                busRouteStationList.add(ctBusRouteStation);
            }
            if (busRouteStationList.size() > 10000) {
                int pageNo = busRouteStationList.size() / 10000;
                for (int index = 1; index <= pageNo; index++) {
                    int startIndex = (index - 1) * 10000;
                    if (index == pageNo) {
                        int endIndex = busRouteStationList.size();
                        List<CtBusRouteStation> busRouteStationList1 = busRouteStationList.subList(startIndex, endIndex);
                        masterBusRouteInfoStationRepository.batchAll(busRouteStationList1);
                    } else {
                        int endIndex = index * 10000;
                        List<CtBusRouteStation> busRouteStationList1 = busRouteStationList.subList(startIndex, endIndex);
                        masterBusRouteInfoStationRepository.batchAll(busRouteStationList1);
                    }

                }
            } else {
                masterBusRouteInfoStationRepository.batchAll(busRouteStationList);
            }


        }
    }

    @Transactional
    public void busStation() {
        long beginAll = System.currentTimeMillis();
        List<SgjjtStop> allBusStation = slaveGjBasicInfo4.findAllBusStation();

        if (!CollectionUtils.isEmpty(allBusStation)) {
            //删除 1001
            //查找自行车站点数据
            List<BaseGeoArea> baseGeoAreaList = masterBaseAreaRepository.findBaseGeoAreaByAreaType(1001L);
            List<GisPoint> allByBusinessCategory = masterGisPointRepository.findAllByBusinessCategory("1001");
            List<BaseGeoAreaDetail> detailList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(baseGeoAreaList)) {
                baseGeoAreaList.forEach((e) -> {
                    BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                    baseGeoAreaDetail.setAreaId(e.getId());
                });
                long begin = System.currentTimeMillis();
                masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(1001L);
                long end = System.currentTimeMillis();
                System.out.println("baseGeo删除时长：" + (end - begin) + "ms");

                long begin1 = System.currentTimeMillis();
                masterBaseGeoAreaDetailRepository.deleteInBatch(detailList);
                long end1 = System.currentTimeMillis();
                System.out.println("baseGeoDetail删除时长：" + (end1 - begin1) + "ms");
            }
            //删除对应表
            if (!CollectionUtils.isEmpty(allByBusinessCategory)) {
                long begin = System.currentTimeMillis();
                masterGisPointRepository.deleteGisPointByBusinessCategory("1001");
                long end = System.currentTimeMillis();
                System.out.println("gisPoint删除时长：" + (end - begin) + "ms");
            }

            List<BaseGeoArea> addBaseGeoList = new ArrayList<>();
            List<GisPoint> addGisPointList = new ArrayList<>();
            List<BaseGeoAreaDetail> addBaseGeoDetailList = new ArrayList<>();
            for (SgjjtStop stop : allBusStation) {
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                baseGeoAreaDetail.setStationType(2L);

                BaseGeoArea baseGeoArea = new BaseGeoArea();
                baseGeoArea.setUpdator("jpa-program");
                baseGeoArea.setUpdateDate(LocalDateTime.now());
                baseGeoArea.setAreaType(1001L);
                baseGeoArea.setAreaCode("gjzd" + stop.getTongid());
                baseGeoArea.setParentId(20873L);
                baseGeoArea.setAreaName(stop.getStationname());
                baseGeoArea.setTrueName(stop.getStationname());
                baseGeoArea.setLatitude(stop.getLatitude().doubleValue());
                baseGeoArea.setLongitude(stop.getLongitude().doubleValue());
                baseGeoArea.setBaseGeoAreaDetail(baseGeoAreaDetail);
                addBaseGeoList.add(baseGeoArea);

            }
            List<BaseGeoArea> targetList = masterBaseAreaRepository.batchBaseGeoAll(addBaseGeoList);
            for (BaseGeoArea baseGeoArea : targetList) {
                BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();
                baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                addBaseGeoDetailList.add(baseGeoAreaDetail);

                GisPoint gisPoint = new GisPoint();
                gisPoint.setObjId(baseGeoArea.getId());
                gisPoint.setYwCategory("1001");
                gisPoint.setObjType(1L);
                gisPoint.setLongitude(baseGeoArea.getLongitude());
                gisPoint.setLatitude(baseGeoArea.getLatitude());
                gisPoint.setName(baseGeoArea.getAreaName());
                gisPoint.setCode(baseGeoArea.getAreaCode());
                gisPoint.setBusinessCategory("1001");
                addGisPointList.add(gisPoint);


            }


            masterBaseGeoAreaDetailRepository.batchBaseGeoDetailAll(addBaseGeoDetailList);

            masterGisPointRepository.batchGisPointDetailAll(addGisPointList);
            long endAll = System.currentTimeMillis();
            System.out.println("总耗时：" + (endAll - beginAll) / 1000 + "秒");
        }


    }

    @Transactional
    public void busStopStation() {
        List<BaseGeoAreaDetail> removeList = new ArrayList<>();
        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(1004L);
        if (!CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            for (BaseGeoArea baseGeoAreaDetail : baseGeoAreaByAreaType) {
                BaseGeoAreaDetail baseGeoAreaDetail1 = new BaseGeoAreaDetail();
                baseGeoAreaDetail1.setAreaId(baseGeoAreaDetail.getId());
                removeList.add(baseGeoAreaDetail1);
            }
            masterBaseGeoAreaDetailRepository.deleteAll(removeList);
            baseGeoAreaByAreaType.clear();
        }
        //查询出应急库内停保基地  1004
        List<BaseGeoArea> byAreaType = masterBaseAreaRepository.findByAreaType(1004L);

        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(1004L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("1004");


        List<SgjjtStation> allBusStation = slaveGjBasicInfo3.findAllBusStation();


        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(allBusStation)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                List<BaseGeoAreaDetail> baseGeoAreaListDetail = new ArrayList<>();
                List<GisPoint> gisPointList = new ArrayList<>();
                for (SgjjtStation sgjjtStation : allBusStation) {
                    BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                    baseGeoAreaDetail.setTotalNum(Long.valueOf(sgjjtStation.getTotalNum()));
                    baseGeoAreaDetail.setOilNum(Long.valueOf(sgjjtStation.getOilNum()));
                    baseGeoAreaDetail.setLngNum(Long.valueOf(sgjjtStation.getLngNum()));
                    baseGeoAreaDetail.setElectricNum(Long.valueOf(sgjjtStation.getElectricNum()));
                    baseGeoAreaDetail.setTracklessNum(Long.valueOf(sgjjtStation.getTracklessNum()));
                    baseGeoAreaDetail.setDiscardNum(Long.valueOf(sgjjtStation.getDiscardNum()));
                    baseGeoAreaDetail.setIsTrue(0L);
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    List<String> collect = byAreaType.stream().filter(v ->
                            sgjjtStation.getBaseName().equals(v.getAreaName()))
                            .map(BaseGeoArea::getAreaCode)
                            .collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(collect)){
                        baseGeoArea.setAreaCode(collect.get(0));
                    }else {
                        baseGeoArea.setAreaCode("gjtbjd" + sgjjtStation.getTongid());
                    }
                    baseGeoArea.setAreaName(sgjjtStation.getBaseName());
                    baseGeoArea.setAreaType(1004L);
                    baseGeoArea.setParentId(20940L);
                    baseGeoArea.setLongitude(sgjjtStation.getLon().doubleValue());
                    baseGeoArea.setLatitude(sgjjtStation.getLat().doubleValue());
                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getAddress());
                    baseGeoArea.setBaseGeoAreaDetail(baseGeoAreaDetail);
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);

                for (BaseGeoArea baseGeoArea : baseGeoAreaList) {
                    BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();
                    baseGeoAreaDetail.setAreaId(baseGeoArea.getId());
                    baseGeoAreaListDetail.add(baseGeoAreaDetail);
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("1004");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("1004");
                    gisPointList.add(gisPoint);
                }
                masterGisPointRepository.batchGisPointDetailAll(gisPointList);
                masterBaseGeoAreaDetailRepository.batchBaseGeoDetailAll(baseGeoAreaListDetail);
            }


        } else {
            if (!CollectionUtils.isEmpty(allBusStation)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (SgjjtStation sgjjtStation : allBusStation) {
                    String baseName = sgjjtStation.getBaseName();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            baseGeoArea.setLongitude(sgjjtStation.getLon().doubleValue());
                            baseGeoArea.setLatitude(sgjjtStation.getLat().doubleValue());
                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getAddress());
                            BaseGeoAreaDetail baseGeoAreaDetail = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(baseGeoArea.getId());
                            baseGeoAreaDetail.setTotalNum(Long.valueOf(sgjjtStation.getTotalNum()));
                            baseGeoAreaDetail.setOilNum(Long.valueOf(sgjjtStation.getOilNum()));
                            baseGeoAreaDetail.setLngNum(Long.valueOf(sgjjtStation.getLngNum()));
                            baseGeoAreaDetail.setElectricNum(Long.valueOf(sgjjtStation.getElectricNum()));
                            baseGeoAreaDetail.setTracklessNum(Long.valueOf(sgjjtStation.getTracklessNum()));
                            baseGeoAreaDetail.setDiscardNum(Long.valueOf(sgjjtStation.getDiscardNum()));
                            baseGeoAreaDetail.setIsTrue(0L);
                            baseGeoArea.setBaseGeoAreaDetail(baseGeoAreaDetail);
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                        baseGeoAreaDetail.setTotalNum(Long.valueOf(sgjjtStation.getTotalNum()));
                        baseGeoAreaDetail.setOilNum(Long.valueOf(sgjjtStation.getOilNum()));
                        baseGeoAreaDetail.setLngNum(Long.valueOf(sgjjtStation.getLngNum()));
                        baseGeoAreaDetail.setElectricNum(Long.valueOf(sgjjtStation.getElectricNum()));
                        baseGeoAreaDetail.setTracklessNum(Long.valueOf(sgjjtStation.getTracklessNum()));
                        baseGeoAreaDetail.setDiscardNum(Long.valueOf(sgjjtStation.getDiscardNum()));
                        baseGeoAreaDetail.setIsTrue(0L);
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("gjtbjd" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getBaseName());
                        baseGeoArea.setAreaType(1004L);
                        baseGeoArea.setParentId(20940L);
                        baseGeoArea.setLongitude(sgjjtStation.getLon().doubleValue());
                        baseGeoArea.setLatitude(sgjjtStation.getLat().doubleValue());
                        baseGeoArea.setUpdator("jpa-program");
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getAddress());
                        baseGeoArea.setBaseGeoAreaDetail(baseGeoAreaDetail);
                        addBaseList.add(baseGeoArea);
                    }
                }

                if (!CollectionUtils.isEmpty(addBaseList)) {
                    for (BaseGeoArea baseGeoArea : addBaseList) {
                        BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                        BaseGeoAreaDetail baseGeoAreaDetail = baseGeoArea.getBaseGeoAreaDetail();
                        baseGeoAreaDetail.setAreaId(save.getId());
                        masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
                        GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(baseGeoArea.getId());
                        if (distinctByObjId == null) {
                            GisPoint gisPoint = new GisPoint();
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("1004");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("1004");
                            masterGisPointRepository.save(gisPoint);
                        } else {
                            GisPoint gisPoint = distinctByObjId;
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("1004");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("1004");
                            masterGisPointRepository.save(gisPoint);
                        }

                    }
                }

            }


        }


    }


    @Transactional
    public void busElectricStop() {
        //查询出站牌数 1023


        masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(1023L);
        masterGisPointRepository.deleteGisPointByBusinessCategory("1023");

        List<BaseGeoArea> baseGeoAreaByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(1023L);
        List<SgjjtElectricStop> all = slaveGjBasicInfo5.findAllByOp("insert");
        if (CollectionUtils.isEmpty(baseGeoAreaByAreaType)) {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> baseGeoAreaList = new ArrayList<>();
                List<GisPoint> gisPointList = new ArrayList<>();
                for (SgjjtElectricStop sgjjtStation : all) {
                    BaseGeoArea baseGeoArea = new BaseGeoArea();
                    baseGeoArea.setAreaCode("gjdzzp" + sgjjtStation.getTongid());
                    baseGeoArea.setAreaName(sgjjtStation.getStopname());
                    baseGeoArea.setAreaType(1023L);
                    //baseGeoArea.setParentId(20940L);
                    baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                    baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                    baseGeoArea.setUpdator("jpa-program");
                    baseGeoArea.setUpdateDate(LocalDateTime.now());
                    baseGeoArea.setAddress(sgjjtStation.getStopaddress());
                    baseGeoArea.setExtension1(String.valueOf(sgjjtStation.getStatus()));
                    baseGeoAreaList.add(baseGeoArea);
                }
                masterBaseAreaRepository.batchBaseGeoAll(baseGeoAreaList);

                for (BaseGeoArea baseGeoArea : baseGeoAreaList) {
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("1023");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("1023");
                    gisPointList.add(gisPoint);
                }
                masterGisPointRepository.batchGisPointDetailAll(gisPointList);
            }


        } else {
            if (!CollectionUtils.isEmpty(all)) {
                List<BaseGeoArea> addBaseList = new ArrayList<>();
                for (SgjjtElectricStop sgjjtStation : all) {
                    String baseName = sgjjtStation.getStopname();
                    boolean update = false;
                    for (BaseGeoArea baseGeoArea : baseGeoAreaByAreaType) {
                        String areaName = baseGeoArea.getAreaName();
                        if (baseName.equals(areaName)) {
                            baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                            baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                            baseGeoArea.setUpdator("jpa-program");
                            baseGeoArea.setUpdateDate(LocalDateTime.now());
                            baseGeoArea.setAddress(sgjjtStation.getStopaddress());
                            baseGeoArea.setExtension1(String.valueOf(sgjjtStation.getStatus()));
                            addBaseList.add(baseGeoArea);
                            update = true;
                            break;
                        }
                    }
                    if (!update) {
                        BaseGeoArea baseGeoArea = new BaseGeoArea();
                        baseGeoArea.setAreaCode("gjdzzp" + sgjjtStation.getTongid());
                        baseGeoArea.setAreaName(sgjjtStation.getStopname());
                        baseGeoArea.setAreaType(1023L);
                        baseGeoArea.setParentId(20940L);
                        baseGeoArea.setLongitude(sgjjtStation.getLongitude().doubleValue());
                        baseGeoArea.setLatitude(sgjjtStation.getLatitude().doubleValue());
                        baseGeoArea.setUpdator("jpa-program");
                        baseGeoArea.setUpdateDate(LocalDateTime.now());
                        baseGeoArea.setAddress(sgjjtStation.getStopaddress());
                        baseGeoArea.setExtension1(String.valueOf(sgjjtStation.getStatus()));
                        addBaseList.add(baseGeoArea);
                    }
                }

                if (!CollectionUtils.isEmpty(addBaseList)) {
                    for (BaseGeoArea baseGeoArea : addBaseList) {
                        BaseGeoArea save = masterBaseAreaRepository.save(baseGeoArea);
                        GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(baseGeoArea.getId());
                        if (distinctByObjId == null) {
                            GisPoint gisPoint = new GisPoint();
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("1023");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("1023");
                            masterGisPointRepository.save(gisPoint);
                        } else {
                            GisPoint gisPoint = distinctByObjId;
                            gisPoint.setObjId(baseGeoArea.getId());
                            gisPoint.setYwCategory("1023");
                            gisPoint.setObjType(1L);
                            gisPoint.setLongitude(baseGeoArea.getLongitude());
                            gisPoint.setLatitude(baseGeoArea.getLatitude());
                            gisPoint.setName(baseGeoArea.getAreaName());
                            gisPoint.setCode(baseGeoArea.getAreaCode());
                            gisPoint.setBusinessCategory("1023");
                            masterGisPointRepository.save(gisPoint);
                        }

                    }
                }

            }


        }

    }
}
